<?php
/**
 * Created by PhpStorm.
 * User: lyang
 * Date: 2016/12/31
 * Time: 17:19
 */

defined("ROOT") or exit("access deny");

$db      = YC::make("DB")->getPdo();
YC::set("TABLE","yc_menu");
YC::set("DB",$db);
switch ($_GET["_method_"]) {
    case "get_rows":
        get_article_list();
        break;
    case "edit_rows":
        edit_article();
        break;
}

function get_article_list()
{
    $db = YC::get("DB");
    $table = YC::get("TABLE");
    $rows=20;
    $request=$_GET;


    $page=isset($request["page"])?$request["page"]:1;
    $sord=isset($request["sord"])?$request["sord"]:"asc";
    $sidx=empty($request["sidx"])? "":" order by ".$request["sidx"]." ".$request["sord"] ;


    $where=" 1 ";
    if(isset($request["filters"])){
        $filters=json_decode($request["filters"],true);
        if($and=$filters["groupOp"]){
            foreach($filters["rules"] as $k=>$v){
                $where = $where.$and." ".op_($v["field"],$v["op"],$v["data"])." ";
            }
        }
    }
    $limit=(($page-1)*20).",20";
    $sql1 = "select count(*) as records from $table where $where ";
    $query	=	$db->query($sql1);
    $data	=	$query->fetch(PDO::FETCH_ASSOC);
    $data["records"]=intval($data["records"]);
    $data["total"]	=	ceil($data["records"]/$rows);
    $data["page"]	=	$page;


    $sql="select * from $table where $where $sidx  limit $limit";
    $query	=	$db->query($sql);
    $res	=	$query->fetchAll(PDO::FETCH_ASSOC);
    foreach($res as $k=>$v){
        $data["rows"][$k]["id"]=$v["id"];
        $data["rows"][$k]["cell"]=$v;
    }

    YC::json($data);
}

function op_($k, $o, $v)
{
    $op   = [
        "eq" => " = ",
        "ne" => " != ",
        "lt" => " < ",
        "le" => " <= ",
        "gt" => " > ",
        "ge" => " >= ",
        "cn" => "like",
        "nc" => "not like",
    ];
    $mark = $op[$o];
    if ($mark == "like") {
        return " " . $k . " like '%" . $v . "%' ";
    } else {
        if ($mark == "not like") {
            return " " . $k . " not like '%" . $v . "%' ";
        } else {
            return " " . $k . "" . $mark . "" . $v . " ";
        }
    }

}

/*
*/
function edit_article()
{
    $request = $_POST;

    $type = isset($request["oper"]) ? $request["oper"] : die;
    $re = 0;
    switch ($type) {
        case "add":
            $re = add($request);
            break;
        case "del":
            $re = del($request);
            break;
        case "edit":
            $re = edit($request);
            break;
    }
    YC::json(["result"=>$re]);
}

function del($data)
{
    $table = YC::get("TABLE");
    $id  = isset($data["id"]) ? $data["id"] : die;
    return YC::make("DB")->delete($table,["id[=]"=>$id]);
}

function add($data)
{
    $table = YC::get("TABLE");
    unset($data["id"]);
    unset($data["oper"]);
    return YC::make("DB")->insert($table,$data);
}

function edit($data)
{
    $table = YC::get("TABLE");
    $id    = isset($data["id"]) ? $data["id"] : exit("die");
    unset($data["id"]);
    unset($data["oper"]);
    return YC::make("DB")->update($table,$data,["id[=]"=>$id]);

}